package com.example.pharmacypick.service.impl;

import com.example.pharmacypick.dto.PatientDto;
import com.example.pharmacypick.mapper.PatientMapper;
import com.example.pharmacypick.pojo.Patient;
import com.example.pharmacypick.service.PatientService;
import com.example.pharmacypick.vo.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    PatientMapper patientMapper;

    @Override
    public PageInfo<Patient> findAllPatients(PatientDto patientDto) {
        try(Page<Object> pages = PageHelper.startPage(patientDto.getPageNum(), patientDto.getPageSize())) {

            //根据数据查询
            List<Patient> allPatients = patientMapper.findAllPatients(patientDto);
            return new PageInfo<>(pages.getTotal(),allPatients);
        }
    }

    @Override
    public void addPatient(PatientDto patientDto) {
        Patient patient=new Patient();
        BeanUtils.copyProperties(patientDto,patient);
        patientMapper.addPatient(patient);
    }

    @Override
    public void updatePatient(PatientDto patientDto) {
        patientMapper.updatePatient(patientDto);
    }

    @Override
    public void deletePatient(Long patientRecord) {
        patientMapper.deletePatient(patientRecord);
    }

    /**
     * 病人登录
     * @param patient 病人
     * @return 返回病人对象
     */
    @Override
    public Patient patientLogin(Patient patient) {
        Patient patientByPatientRecord = patientMapper.findPatientByPatientRecord(patient.getPatientRecord());

        if(patientByPatientRecord==null||!patientByPatientRecord.getPatientName().equals(patient.getPatientName())){
            return null;
        }

        return patientByPatientRecord;
    }

    @Override
    public Patient findPatientByPatientRecord(int patientRecord) {
        Patient patientByPatientRecord = patientMapper.findPatientByPatientRecord(patientRecord);

        return patientByPatientRecord;
    }


}
